package com.trendata.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trendata.domain.Image;
import com.trendata.domain.Like;
import com.trendata.domain.User;
import com.trendata.service.CommentService;
import com.trendata.service.ImageService;
import com.trendata.service.LikeService;
import com.trendata.util.Constants;
import com.trendata.util.JsonResult;

/**
 * 无需登录的图片接口
 * 
 * @author: HuangXiuXiu Email:243559392@qq.com
 */
@Controller
@RequestMapping("/image")
public class ImageController {

	@Autowired
	private ImageService imageService;
	@Autowired
	private LikeService likeService;
	@Autowired
	private CommentService commentService;

	/**
	 * 
	 * 获取店铺内所有公开的照片
	 * <p>
	 * url=image/selectShopPublicImages.do?shopOpenId=100000&no=1&size=10
	 * shopOpenId是查看的店铺的id 。no是第几页，从第一页开始。 size是一页的大小
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @return 返回结果 totalsize 和 shopPublicImages
	 */
	@RequestMapping("/selectShopPublicImages")
	@ResponseBody
	public String selectShopPublicImages(HttpServletRequest request, HttpServletResponse response) {
		JsonResult result = new JsonResult();
		Long shopOpenId = (Long) request.getSession().getAttribute(Constants.SESSION_SHOP_OPEN_ID);// 店铺编号
		Integer type = new Integer(request.getParameter("state"));
		String no = request.getParameter("no");// 第几页
		String size = request.getParameter("size");// 每一页大小
		if (no == null || size == null) {
			return result.setMessage("请传入正确的参数").toJsonString();
		}
		int startIndex = (Integer.valueOf(no) - 1) * Integer.valueOf(size);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int totalSize = imageService.selectCountByParams(null, shopOpenId, type);
		resultMap.put("totalSize", totalSize);
		List<Image> shopPublicImages = imageService.selectByParamters(null, shopOpenId, type, startIndex, Integer.valueOf(size));
		if (shopPublicImages != null && shopPublicImages.size() > 0) {
			for (Image image : shopPublicImages) {
				// 点赞数量
				int likeNum = likeService.selectCountByImageId(image.getId());
				image.setLikeNum(likeNum);
				// 当前登录用户是否点赞
				User user = (User) request.getSession().getAttribute(Constants.SESSION_TYPE_USER);
				if (user != null) {
					Like like = likeService.selectByImageIdAndUserOpenId(image.getId(), user.getOpenId());
					if (like != null) {
						image.setLiked(true);
					} else {
						image.setLiked(false);
					}
				}
				// 评论数量
				int commentNum = commentService.selectCountByImageId(image.getId());
				image.setCommentNum(commentNum);
			}
		}

		resultMap.put("shopPublicImages", shopPublicImages);
		result.setValue(resultMap);
		result.setSuccess(true);
		return result.toJsonString();
	}

	/**
	 * 查看用户公开照片
	 * <p>
	 * url=/image/selectUserPublicImages.do?userOpenId=&no=&size=
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @return totalSize和userPublicImages
	 */
	@RequestMapping("/selectUserPublicImages")
	@ResponseBody
	public String selectUserPublicImages(HttpServletRequest request, HttpServletResponse response) {
		JsonResult result = new JsonResult();
		String userOpenId = request.getParameter("userOpenId");// 用户编号
		String no = request.getParameter("no");// 第几页
		String size = request.getParameter("size");// 每一页大小
		Integer type = new Integer(request.getParameter("state"));
		if (userOpenId == null || userOpenId.equals("") || no == null || size == null) {
			return result.setMessage("请传入正确的参数").toJsonString();
		}
		int startIndex = (Integer.valueOf(no) - 1) * Integer.valueOf(size);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int totalSize = imageService.selectCountByParams(userOpenId, null, type);
		resultMap.put("totalSize", totalSize);
		List<Image> userPublicImages = imageService.selectByParamters(userOpenId, null, type, startIndex, Integer.valueOf(size));

		if (userPublicImages != null && userPublicImages.size() > 0) {
			for (Image image : userPublicImages) {
				// 点赞数量
				int likeNum = likeService.selectCountByImageId(image.getId());
				image.setLikeNum(likeNum);
				// 当前登录用户是否点赞
				User user = (User) request.getSession().getAttribute(Constants.SESSION_TYPE_USER);
				if (user != null) {
					Like like = likeService.selectByImageIdAndUserOpenId(image.getId(), user.getOpenId());
					if (like != null) {
						image.setLiked(true);
					} else {
						image.setLiked(false);
					}
				}
				// 评论数量
				int commentNum = commentService.selectCountByImageId(image.getId());
				image.setCommentNum(commentNum);
			}
		}

		resultMap.put("userPublicImages", userPublicImages);
		result.setValue(resultMap);
		result.setSuccess(true);
		return result.toJsonString();
	}

}
